package com.example.reference_book.controller;

import com.example.reference_book.entity.Book;
import com.example.reference_book.entity.BookTag;
import com.example.reference_book.entity.UserTag;
import com.example.reference_book.service.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

@RestController
@RequestMapping("BookTotalScoreCalculate")
public class BookTotalScoreCalculate implements BookRefScore {

    @Resource
    private BookTagService bookTagService;

    @Resource
    private UserTagService userTagService;

    @Resource
    private BookService bookService;

    //实现分区推荐 根据用户标签和书籍标签。
    @GetMapping("{userid}")
    @Override
    public Map<Integer, List<Book>> refscore(@PathVariable("userid") Integer userid) {
        //查看该用户关注了哪些标签

        List<UserTag> userTagList = this.userTagService.getusertag(userid);
        System.out.println("用户关注了这些标签：");
        for (int i = 0; i < userTagList.size(); i++) {
            System.out.println(userTagList.get(i).getTagId());
        }

        System.out.println("");
        HashMap a = new HashMap();
        Map<Integer, List<BookTag>> map = new HashMap<Integer, List<BookTag>>();
        for (int i = 0; i < userTagList.size(); i++) {
            //先获取用户感兴趣了什么标签，然后用这个标签查找符合的相关书籍，有很多本 在booktag里用tagid查找
            List<BookTag> bookTagList = bookTagService.queryByTagId(userTagList.get(i).getTagId());

            for (int j = 0; j < bookTagList.size(); j++) {
                System.out.println(bookTagList.get(j).getBookId());
            }
            System.out.println("");

            if (bookTagList.size()!=0) {
                map.put(i, bookTagList);
            } else{
                System.out.println("这个标签暂时没有对应的书籍");
                continue;
            }

        }

        System.out.println("---bookid----");
        //把书籍列表按照标签降序排列 最终取前六
        for (int i = 0; i < map.size(); i++) {
            System.out.println(map.get(i).get(0).getBookId());
            Collections.sort(map.get(i), new Comparator<BookTag>() {
                @Override
                public int compare(BookTag o1, BookTag o2) {
                    return o2.getNum().compareTo(o1.getNum());
                }
            });
        }

        System.out.println("----按照书标签的num排序之后找到这本书并且返回 ----");
        Map<Integer, List<Book>> bookmap = new HashMap<Integer, List<Book>>();
        for (int i = 0; i < map.size(); i++) {
            List<Book> bookList = new ArrayList<>();
            for (int j = 0; j < map.get(i).size(); j++) {
                System.out.println(map.get(i).get(j).getBookId());
                bookList.add(bookService.queryById(map.get(i).get(j).getBookId()));
            }
            bookmap.put(i,bookList);
        }

        return bookmap;
    }
}
